<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:general="http://java.sun.com/jsf/composite/components/general"
      >
    <ui:composition template="template.xhtml">
            
        <ui:define name="dialogs">

              <general:confirmDialog
                widgetDialog="confirmRemoveDialog"
                dialogTitle="Remoção de nota"
                message="Deseja remover a nota selecionada?"
                buttonYesAction="#{notaFiscalBean.remove()}"
                onclick_no_javascript="confirmRemoveBankDialog.hide()"
                onclick_yes_javascript="confirmRemoveBankDialog.hide()"
                onclick_yes_update=":listNotasform :growl"/>
            
            <p:dialog header="Valores" widgetVar="valoresDialog" resizable="false" modal="true" width="600" >
                <h:form id="valoresForm">
                    <p:panel header="Totalização da nota fiscal">
                           <script type="text/javascript">
                                 apply_price_format('.valor');
                                 apply_quant_format('.quant');

                           </script>
                        <h:panelGroup id="valores">
                        <h:panelGrid columns="2">
                             <h:outputText value="Pagamento:" styleClass="formShortLabel"/>
                             <p:selectOneMenu value="#{notaFiscalBean.pedidoSelected.tipoPagamento}" disabled="true" style="width: 182px" converter="tipoPagamentoConverter" >
                                   <f:selectItem itemLabel="Escolha o tipo de pagamento" itemValue="" />
                                   <f:selectItems value="#{pedidoBean.tipoPagamentos}" var="tipos"
                                                  itemLabel="#{tipos.name}" itemValue="#{tipos}"/>
                               </p:selectOneMenu>


                             <h:outputText value="Valor frete:" styleClass="formShortLabel"/>
                             <p:inputText value="#{notaFiscalBean.pedidoSelected.valorFrete}" styleClass="valor"
                                          converter="monetaryConverter" disabled="true" />

                             <h:outputText value="Datas pagamento:" styleClass="formShortLabel"/>
                             <p:inputText value="#{notaFiscalBean.pedidoSelected.datasPagamento}" disabled="true" />
                             </h:panelGrid>
                            <br clear="none" />

                             <h:panelGrid columns="2">
                             <h:outputText value="Valor total:" styleClass="formShortLabel"/>
                             <p:inputText id="valorTotal" value="#{notaFiscalBean.nota.valorTotal}" styleClass="valor" converter="monetaryConverter" disabled="true" />
                             </h:panelGrid>

                        </h:panelGroup>
                     <br clear="none" />
                     </p:panel>
                     <br clear="none" />
                </h:form>
            </p:dialog>

            <p:dialog header="Novo titulo" widgetVar="tituloDialog" resizable="false" modal="true" width="600" >
                <h:form id="newTituloForm">
                <p:panel header="Cadastro de titulo"  >
                                 <script type="text/javascript">
                                     apply_price_format('.valor');
                                     apply_quant_format('.quant');
                                 </script>
                                <h:panelGrid columns="2">
                                    <h:outputText value="Origem:" styleClass="formShortLabel"/>
                                    <p:inputText value="#{notaFiscalBean.pedidoSelected.obra}" disabled="true" size="66"/>
                                    <h:outputText value="Fornecedor:" styleClass="formShortLabel"/>
                                    <p:inputText value="#{notaFiscalBean.pedidoSelected.fornecedor}" disabled="true" size="66"/>
                                </h:panelGrid>

                                <h:panelGrid columns="4">
                                    <h:outputText value="Nota:" styleClass="formShortLabel"/>
                                    <p:inputText id="numeroNota" value="#{notaFiscalBean.nota.number}" disabled="true"/>

                                    <h:outputText value="Data movimento:" styleClass="formShortLabel"/>
                                    <p:calendar value="#{notaFiscalBean.titulo.emissao}" />

                                    <h:outputText value="Data vencimento:" styleClass="formShortLabel"/>
                                    <p:calendar value="#{notaFiscalBean.titulo.vencimento}" />

                                    <h:outputText value="Valor:" styleClass="formShortLabel"/>
                                    <p:inputText value="#{notaFiscalBean.titulo.valor}" styleClass="valor"
                                          converter="monetaryConverter" />
                                </h:panelGrid>
                                   <p:watermark for="numeroNota" value="Numero nota" />
                                <h:panelGrid columns="3">
                                    <h:outputText value="Pago:" styleClass="formShortLabel" />
                                    <p:selectBooleanCheckbox  value="#{notaFiscalBean.titulo.isPago}" />
                                    <h:outputText value="Marcar se estiver pago."/>
                                </h:panelGrid>

                                <h:panelGrid columns="2">
                                   <h:outputText value="Tipo pagamento" styleClass="formShortLabel" />
                                   <p:selectOneMenu value="#{notaFiscalBean.titulo.tipoPagamento}" style="width: 182px" converter="tipoPagamentoConverter"  >
                                       <f:selectItem itemLabel="Escolha o tipo de pagamento" itemValue="" />
                                       <f:selectItems value="#{notaFiscalBean.tipoPagamentos}" var="tipos"
                                                      itemLabel="#{tipos.name}" itemValue="#{tipos}"/>
                                   </p:selectOneMenu>
                                </h:panelGrid>

                                <h:panelGrid columns="2">
                                    <h:outputText value="Referente:" styleClass="formShortLabel"/>
                                    <p:inputTextarea value="#{notaFiscalBean.titulo.referente}" autoResize="false" cols="51"/>
                                </h:panelGrid>
                                <h:panelGrid columns="2">
                                    <h:outputText value="Obs:" styleClass="formShortLabel"/>
                                    <p:inputTextarea value="#{notaFiscalBean.titulo.obs}" autoResize="false" cols="51"/>
                                </h:panelGrid>

                            </p:panel>
                            <p:panel header="Conta"  >
                                <h:panelGrid columns="2">
                                <h:outputText value="Banco:" styleClass="formShortLabel"/>
                                <p:selectOneMenu value="#{notaFiscalBean.titulo.bank}" >
                                    <f:selectItem itemLabel="Escolha um banco" itemValue="" />
                                    <f:selectItems value="#{notaFiscalBean.banks}" var="b"
                                                      itemLabel="#{b.name}" itemValue="#{b}"/>
                                   </p:selectOneMenu>

                                 <h:outputText value="Agencia:" styleClass="formShortLabel"/>
                                 <p:inputText value="#{notaFiscalBean.titulo.agencia}" />

                                 <h:outputText value="Conta:" styleClass="formShortLabel"/>
                                 <p:inputText value="#{notaFiscalBean.titulo.conta}" />

                                </h:panelGrid>
                            </p:panel>
                    <p:separator />
                    <p:commandButton value="Salvar" action="#{notaFiscalBean.addTituloInList()}" update=":newNotaForm :growl" onclick="tituloDialog.hide()"  />
                </h:form>
            </p:dialog>


            <p:dialog header="Nova nota" widgetVar="newNotaDialog" resizable="false" modal="true" width="730" >
              <h:form id="newNotaForm">
                <p:tabView>
                  <p:tab title="Dados">
                    <p:panel header="Fornecedor"  >
                                <h:panelGrid columns="2">
                                  <h:outputText value=" Nome:  " styleClass="formShortLabel"/>
                                  <p:autoComplete value="#{notaFiscalBean.fornecedorSelected}"
                                                completeMethod="#{autoCompleteBean.completeFornecedor}"
                                                var="f" itemLabel="#{f.name}" itemValue="#{f}"
                                                converter="fornecedorConverter" size="90" >
                                      <p:ajax event="itemSelect" listener="#{notaFiscalBean.fornecedorSelectListener}"
                                              update="dadosFornecedor :growl"/>
                                  </p:autoComplete>

                                </h:panelGrid>
                                <h:panelGroup id="dadosFornecedor">
                                <h:panelGrid columns="4">
                                    <h:outputText value="Razão Social:" styleClass="formShortLabel"/>
                                    <p:inputText value="#{notaFiscalBean.fornecedorSelected.razaoSocial}"  style="width: 250px" disabled="true" />
                                     <h:outputText value="CNPJ:" styleClass="formShortLabel"/>
                                     <p:inputText value="#{notaFiscalBean.fornecedorSelected.cnpj}"  style="width: 137px" disabled="true" />
                                </h:panelGrid>
                                </h:panelGroup>
                             </p:panel>

                            <p:panel header="Empresa"  >
                                <h:panelGrid columns="2">
                                  <h:outputText value=" Nome:  " styleClass="formShortLabel"/>
                                  <p:autoComplete value="#{notaFiscalBean.empresaSelected}"
                                                  completeMethod="#{autoCompleteBean.completeEmpresa}"
                                                var="e" itemLabel="#{e.name}" itemValue="#{e}"
                                                converter="empresaConverter" size="90" >
                                      <p:ajax event="itemSelect" listener="#{notaFiscalBean.empresaSelectListener}"
                                              update="dadosEmpresa :growl"/>
                                  </p:autoComplete>

                                </h:panelGrid>
                                <h:panelGroup id="dadosEmpresa">
                                <h:panelGrid columns="4">
                                    <h:outputText value="Razão Social:" styleClass="formShortLabel"/>
                                    <p:inputText value="#{notaFiscalBean.empresaSelected.razaoSocial}"  style="width: 250px" disabled="true" />
                                     <h:outputText value="CNPJ:" styleClass="formShortLabel"/>
                                     <p:inputText value="#{notaFiscalBean.empresaSelected.cnpj}"  style="width: 137px" disabled="true" />
                                </h:panelGrid>
                                </h:panelGroup>

                            </p:panel>

                            <p:panel header="Dados da Nota"  >

                                <h:panelGrid columns="2">
                                  <h:outputText value=" Tipo Movimento:" styleClass="formShortLabel"/>
                                  <p:autoComplete value="#{notaFiscalBean.nota.tipoMovimento}"
                                                  completeMethod="#{autoCompleteBean.completeTipoMovimento}"
                                                var="n" itemLabel="#{n.name}" itemValue="#{n}"
                                                converter="tipoMovimentoConverter" size="90" />

                                  <h:outputText value="Documento:" styleClass="formShortLabel"/>
                                  <p:autoComplete value="#{notaFiscalBean.nota.tipoDocumento}"
                                                  completeMethod="#{autoCompleteBean.completeTipoDocumento}"
                                                var="n" itemLabel="#{n.name}" itemValue="#{n}"
                                                converter="tipoDocumentoConverter" size="90" />
                                </h:panelGrid>

                                <h:panelGrid columns="6">
                                    <h:outputText value="Numero nota:" styleClass="formShortLabel"/>
                                    <p:inputText value="#{notaFiscalBean.nota.number}"  size="9"/>

                                    <h:outputText value="Data emissão:" styleClass="formShortLabel" />
                                    <p:calendar value="#{notaFiscalBean.nota.dataEmissao}" size="14"/>

                                    <h:outputText value="Data entrega:" styleClass="formShortLabel" />
                                    <p:calendar value="#{notaFiscalBean.nota.dataEntrega}" size="14"/>


                                </h:panelGrid>

                                <h:panelGrid columns="2" >
                                     <h:outputText value="Obs:" styleClass="formShortLabel"/>
                                     <p:inputTextarea value="#{notaFiscalBean.nota.obs}" autoResize="false" cols="68" />
                                </h:panelGrid>
                            </p:panel>

                   </p:tab>

                    <p:tab title="Pedido ">
                      <p:panel header="Insumos"  >

                           <h:panelGrid columns="2">
                              <h:outputText value="Numero pedido:" styleClass="formLabel" style="width: 120px"/>
                              <p:autoComplete id="numeroPedido" value="#{notaFiscalBean.pedidoSelected}"
                                              completeMethod="#{autoCompleteBean.completePedido}"
                                              var="p" itemLabel="#{p.number}" itemValue="#{p}"
                                                converter="pedidoConverter" size="70" >
                                  <p:ajax event="itemSelect" listener="#{notaFiscalBean.pedidoSelectListener}"
                                              update="dadosNotaFiscal :growl"/>
                                </p:autoComplete>

                              <p:watermark for="numeroPedido" value="Digite o numero do pedido" />
                           </h:panelGrid>

                        <h:panelGroup id="dadosNotaFiscal">
                          
                            <p:dataTable var="products" value="#{notaFiscalBean.listProdutos_pedidos()}"
                                 paginator="true" paginatorPosition="bottom" rows="6"
                                 emptyMessage="Nenhum produto foi encontrado"
                                 currentPageReportTemplate="({currentPage} de {totalPage})"
                                 paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">

                                    <p:column headerText="Nome" >
                                        <h:outputText value="#{products.produto.name}" />
                                    </p:column>
                                    <p:column headerText="Quantidade" >
                                        <h:outputText value="#{products.quantidade}" />
                                    </p:column>
                                    <p:column headerText="Unidade" >
                                        <h:outputText value="#{products.produto.unidadeMed.name}" />
                                    </p:column>
                                    <p:column headerText="Valor" >
                                        <h:outputText value="#{products.valor}" />
                                    </p:column>
                                    <p:column headerText="Entregue"  >
                                           <p:selectBooleanButton value="#{products.isDelivered}" onLabel="Sim" offLabel="Não"
                                                                  onIcon="ui-icon-check" offIcon="ui-icon-close"  >
                                               <p:ajax listener="#{notaFiscalBean.cauculeValorNotaSelectListener()}" update=":growl" />
                                           </p:selectBooleanButton>
                                    </p:column>
                              </p:dataTable>
                        </h:panelGroup>

                          <p:separator />
                          <p:commandButton value="Valores" update=":valoresForm" oncomplete="valoresDialog.show()" />
                      </p:panel>

                    </p:tab>

                    <p:tab title="Titulo" id="tabTitulos">
                            <h:panelGroup id="titulosList">
                                <p:dataList value="#{notaFiscalBean.titulos}" var="t" style="width:370px" >
                                    <p:column >
                                        <p:commandButton icon="ui-icon ui-icon-minus" title="Remover titulo"
                                                       
                                                         style="margin-right: 10px">
                                            <f:setPropertyActionListener value="#{t}" target="#{notaFiscalBean.tituloSelected}"/>
                                        </p:commandButton>
                                    </p:column>
                                    <p:column>
                                        <h:outputText value="Doc.: #{t.numeroDocumento}, valor:  #{t.valor}, Vencimento: #{t.vencimento} ">
                                            <f:convertDateTime pattern="dd/MM/yyyy" />
                                        </h:outputText>
                                    </p:column>

                                     <f:facet name="footer">
                                           <p:commandButton icon="ui-icon ui-icon-plus" title="Adicionar titulo"
                                                      update=":newTituloForm" oncomplete="tituloDialog.show()" />

                                     </f:facet>
                               </p:dataList>
                            </h:panelGroup>
                    </p:tab>

               </p:tabView>
                   <p:separator />
                  <p:commandButton value="Salvar" action="#{notaFiscalBean.save()}" update=":listNotasform :growl" onclick="newNotaDialog.hide()"  />
             </h:form>
            </p:dialog>
        </ui:define>

        <ui:define name="content">
            
           <h:form id="listNotasform">
              <h:panelGrid columns="2">
                        <h:outputText value="Numero nota:" styleClass="formLabel" style="width: 120px"/>
                        <p:inputText id="numeroNota" style="width: 300px" value="#{notaFiscalBean.numeroNotaFilter}"
                                     onclick="jQuery(this).select()">
                            <p:ajax event="keyup" update="notaList" />
                        </p:inputText>

                        <p:watermark for="numeroNota" value="Digite o numero da nota" />
                    </h:panelGrid>
              <h:panelGroup id="notaList">
                  <p:dataTable var="n" value="#{notaFiscalBean.listNotas()}"
                               paginator="true" rows="6" rowKey="#{n.id}"
                                     emptyMessage="Nenhuma nota foi encontrada"
                                     selectionMode="single" selection="#{notaFiscalBean.notaSelected}">

                            <p:ajax event="rowSelect" update="@this"/>

                            <f:facet name="header">
                                Lista de notas
                            </f:facet>

                            <p:column style="width: 20%">
                                <f:facet name="header">
                                    <h:outputText value="Numero" />
                                </f:facet>
                                <h:outputText value="#{n.number}" />
                            </p:column>

                             <p:column>
                                <f:facet name="header">
                                    <h:outputText value="Fornecedor" />
                                </f:facet>
                                 <h:outputText value="#{n.fornecedor}" />
                            </p:column>
                            <p:column style="width: 30%">
                                <f:facet name="header">
                                    <h:outputText value="Data emissão" />
                                </f:facet>
                                <h:outputText value="#{n.dataEmissao}" >
                                     <f:convertDateTime pattern="dd/MM/yyyy" />
                                </h:outputText>
                            </p:column>
                               <f:facet name="footer">
                                <p:commandButton icon="ui-icon ui-icon-plus" title="Adicionar nota"
                                                 disabled="#{not sharedPermissionManagedBean.hasPermission('CRIAR_NOTA')}" update=":newNotaForm" oncomplete="newNotaDialog.show()" />
                                <p:commandButton icon="ui-icon ui-icon-minus" title="Remover nota" onclick="confirmRemoveDialog.show()"
                                                 disabled="#{(not sharedPermissionManagedBean.hasPermission('REMOVER_NOTA')) or (notaFiscalBean.notaSelected.id == null)}"/>
                               </f:facet>
                  </p:dataTable>
              </h:panelGroup>
           </h:form>


        </ui:define>

        
    </ui:composition>
</html>


